package net.wofly.right.service.impl;

import net.wofly.common.condition.Condition;
import net.wofly.common.condition.ConditionRelate;
import net.wofly.common.condition.ConditionUtil;
import net.wofly.common.condition.FilterCondition;
import net.wofly.common.condition.Operation;
import net.wofly.common.json.XiNiaoJSON;
import net.wofly.common.persistence.PageWrapper;
import net.wofly.common.util.Identities;
import net.wofly.common.util.PageHelper;
import net.wofly.common.util.ResultCode;
import net.wofly.common.util.ReturnBean;
import net.wofly.right.domain.FuncList;
import net.wofly.right.dto.FuncModuleDto;
import net.wofly.right.repository.FuncListRepository;
import net.wofly.right.service.IFuncListService;
import net.wofly.right.util.FuncModule;
import net.wofly.right.util.FuncType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

@Service("iFuncListService")
@Transactional
public class FuncListServiceImpl implements IFuncListService {
    
    @Autowired
    FuncListRepository funcListRepository;
    
    @Override
    public FuncList save(FuncList entity) {
        return funcListRepository.save(entity);
    }
    
    @Override
    public void delete(FuncList entity) {
        funcListRepository.delete(entity);
    }
    
    @Override
    public void delete(String key) {
        funcListRepository.delete(key);
    }
    
    @Override
    public FuncList findOne(String key) {
        return funcListRepository.findOne(key);
    }
    
    @Override
    public Page<FuncList> findAll(
            Specification<FuncList> specification, Pageable pageable) {
        return funcListRepository.findAll(specification, pageable);
    }
    
    @Override
    public List<FuncList> findByFuncUrl(String funcUrl) {
        return funcListRepository.findByFuncUrl(funcUrl);
    }
    
    @Override
    public Page<FuncList> findByFuncNameAndFuncType(
            String funcName, Integer funcType, Pageable pageable) {
        return funcListRepository.findByFuncNameAndFuncType(funcName, funcType, pageable);
    }
    
    @Override
    public List<FuncList> save(List<FuncList> entityList) {
        return (List<FuncList>) funcListRepository.save(entityList);
    }
    
    @Override
    public ReturnBean<List<FuncModuleDto>> findAllFuncModules() {
        List<FuncModuleDto> dtos = new ArrayList<>();
        FuncModule[] values = FuncModule.values();
        for (FuncModule value : values) {
            FuncModuleDto dto = new FuncModuleDto();
            dto.setEnName(value.getEnName());
            dto.setCnName(value.getCnName());
            dtos.add(dto);
        }
        return new ReturnBean<>(ResultCode.SUCCESS, "查询成功", dtos);
    }
    
    /**
     * 添加功能
     *
     * @param funcList
     * @return
     */
    @Override
    public ReturnBean<FuncList> addFuncList(String funcName,String funcModule,Integer funcType,String funcCode, String funcUrl,String descp) {
        List<FuncList> funcLists = findByFuncUrl(funcUrl);
        if (!CollectionUtils.isEmpty(funcLists)) {
            return new ReturnBean<>(ResultCode.ERROR_SERVICE, "功能的url已经存在，请检查");
        }
        FuncList funcList = new FuncList() ;
        funcList.setFuncName(funcName);
        funcList.setFuncModule(funcModule);
        funcList.setFuncType(funcType);
        funcList.setFuncCode(funcCode);
        funcList.setFuncUrl(funcUrl);
        funcList.setDescp(descp);
        funcList.setFuncListID(Identities.uuid2());
        funcList.setModifyDate(System.currentTimeMillis());
        return new ReturnBean<>(ResultCode.SUCCESS, "添加成功", save(funcList));
    }
    
    /**
     * 修改功能
     *
     * @param funcList
     * @return
     */
    @Override
    public ReturnBean<FuncList> modifyFuncList(FuncList funcList) {
        FuncList dbFuncList = findOne(funcList.getFuncListID());
        if (dbFuncList == null) {
            return new ReturnBean<>(ResultCode.ERROR_SERVICE, "功能的ID不正确");
        }
        
        if (!funcList.getFuncUrl().equals(dbFuncList.getFuncUrl())) {
            List<FuncList> funcLists = findByFuncUrl(funcList.getFuncUrl());
            if (!CollectionUtils.isEmpty(funcLists)) {
                return new ReturnBean<>(ResultCode.ERROR_SERVICE, "功能的url已经存在，请检查");
            }
        }
        
        dbFuncList.setFuncName(funcList.getFuncName());
        dbFuncList.setFuncModule(funcList.getFuncModule());
        dbFuncList.setFuncType(funcList.getFuncType());
        dbFuncList.setFuncCode(funcList.getFuncCode());
        dbFuncList.setFuncUrl(funcList.getFuncUrl());
        dbFuncList.setDescp(funcList.getDescp());
        
        dbFuncList.setModifyDate(System.currentTimeMillis());
        return new ReturnBean<>(ResultCode.SUCCESS, "修改成功", save(dbFuncList));
    }
    
    /**
     * 根据条件查询功能列表
     *
     * @param funcListName
     * @param funcType
     * @return
     */
    @Override
    public ReturnBean<PageWrapper<FuncList>> getFuncListByCond(
            String funcName, Integer funcType, Integer pageNum, Integer pageSize) {
        
        Condition condition = new Condition();
        List<FilterCondition> filterConditions = new ArrayList<FilterCondition>();
        if (!StringUtils.isEmpty(funcName)) {
            filterConditions.add(
                    new FilterCondition(funcName, "funcName", ConditionRelate.and.name(), Operation.like.name(),
                            1));
        }
        if (funcType != null) {
            filterConditions.add(new FilterCondition(String.valueOf(funcType), "funcType", ConditionRelate.and.name(),
                    Operation.eq.name(), 2));
        }
        
        condition.setFilterCondition(filterConditions);
        
        condition = ConditionUtil.buildCondition(XiNiaoJSON.toJSONString(condition), FuncList.class).getResult();
        
        Pageable pageable = new PageRequest(pageNum - 1, pageSize, new Sort(Sort.Direction.ASC, "funcModule"));
        
        Page<FuncList> page = findAll(condition.getSpecification(), pageable);
        if (!CollectionUtils.isEmpty(page.getContent())) {
            for (FuncList funcList : page.getContent()) {
                funcList.setFuncModuleStr(FuncModule.getName(funcList.getFuncModule()));
                funcList.setFuncTypeStr(FuncType.getName(funcList.getFuncType()));
            }
        }
        
        return new ReturnBean<>(ResultCode.SUCCESS, "查询成功", PageHelper.toPage(page));
    }

    @Override
    public Page<FuncList> findPageByFuncType(Integer funcType, Pageable pageable) {
        return funcListRepository.findPageByFuncType(funcType, pageable);
    }
}
